let current = 1;
$().ready(function() {
	validateRule();
    $("#questionText").hide();
    $(".otherType").hide();
    $("#type").val($("#typeHidden").val());
    $("#itemBankType").val($("#itemBankTypeHidden").val());
    $("#resourceType").val($("#resourceTypeHidden").val());

    getVie($("#resourceTypeHidden").val());
    $("#resourceType").change(function () {
        if(current != 1){
            current++;
           // $("#resourceUrl").val("");
            $("#test").html("");
        }
        var type = $(this).val();
        getVie(type)
    });
    //$("#type").change(function () {
        var type = $("#typeHidden").val();
        changeType(type);
    //});
});
function getVie(type) {
    if (type != '') {
        if (type!=0&&type!='') {
            $("#resourse").show();
            $("#btnUpload").show()
        } else {
            $("#resourse").hide();
        }
    } else {
        $("#resourse").hide();
    }
}
function changeType(type) {
    if (type != '' && type!=null && type!=undefined && type!='undefined') {
        if (type!=4&&type!='') {
            $(".otherType").show();
            $("#questionText").hide();
        } else {
            $(".otherType").hide();
            $("#questionText").show();
        }
    } else {
        $(".otherType").hide();
        //$("#questionText").show();
    }
}
layui.use(['upload'], function () {
    var upload = layui.upload;
    upload.render({
        elem: '#btnUpload',
        url: '/jzApi/utils/fileImprot',
        timeout: 36000000,
        size: 1024 * 100,
        accept: 'file',
        // exts: 'docx|doc|xlsx|pdf|ppt',
        done: function (r) {
            $("#resourceUrl").val(r.data);
            $("#test").html(subFileName(r.data));
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
});
$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
$(function () {
    $("#resourceType").change(function () {
        $("#test").html("");
        var type = $(this).val();
        console.log("type:"+type);
        if (type!=0&&type!='') {
            $("#btnUpload").show()
        }
    });
    $("#type").change(function () {
        var type = $(this).val();
        changeType(type);
    });
});
function update() {
    let flag=true;
    let topicTypes = $("#topicTypes").val();
    let options = $("#options").val();
    if(topicTypes == 0 || topicTypes == 1 || topicTypes == 2) {
        if(options == null && options == '' && options == undefined  && options == 'undefined' ){
            flag=false;
        }
    }
    if(flag){
        $.ajax({
            cache : true,
            type : "POST",
            url : "/jzweb/majorItemBank/update",
            data : $('#signupForm').serialize(),// 你的formid
            async : false,
            error : function(request) {
                parent.layer.alert("Connection error");
            },
            success : function(data) {
                if (data.code == 0) {
                    parent.layer.msg("操作成功");
                    parent.reLoad();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);

                } else {
                    parent.layer.alert(data.msg)
                }

            }
        });
    }else {
        parent.layer.alert('请输入答案选项');
    }
}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
            teacherMajorId : {
				required : true
			},
            rightKey : {
                required : true
            },
            type : {
                required : true
            },
            topicContent : {
                required : true
            },
            topicAnalysis : {
                required : false
            },
            score : {
                required : true
            },
		},
		messages : {
            teacherMajorId : {
                required : icon + "请选择教学专业"
            },
            rightKey: {
                required : icon + "请输入正确答案"
            },
            type : {
                required : icon + "请选择题目类型"
            },
            topicContent : {
                required : icon + "请输入题目内容名"
            },
            topicAnalysis : {
                required : icon + "请输入题目解析"
            },
            score : {
                required : icon + "请输入分值"
            }
		}
	})
}